package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Sysnotice;
import cn.wolfcode.trip.base.domain.Syspush;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.mapper.SysnoticeMapper;
import cn.wolfcode.trip.base.mapper.SyspushMapper;
import cn.wolfcode.trip.base.mapper.UserMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.ISysnoticeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SysnoticeServiceImpl implements ISysnoticeService {


    @Autowired
    private SysnoticeMapper sysnoticeMapper;
    @Autowired
    private SyspushMapper syspushMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public void save(Sysnotice sysnotice) {
        sysnotice.setReleasetime(new Date());
        sysnotice.setState(1);
        sysnotice.setType(4);
        sysnotice.setReleasername("获取当前用户 再说吧");

        //  用于系统推送后台显示
        Syspush syspush = new Syspush();
        syspush.setReleasetime(new Date());
        syspush.setReleasename("当前用户吧");
        syspush.setTitle(sysnotice.getTitle());
        syspush.setContent(sysnotice.getContent());
        syspushMapper.insert(syspush);

        List<User> list = userMapper.selectAllUserId();
        for (User user : list) {
            sysnotice.setUser_id(user.getId());
            sysnoticeMapper.insert(sysnotice);
        }
    }

    @Override
    public PageInfo query(QueryObject qo) {

        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        List<Syspush> list = syspushMapper.selectForlist(qo);
        return new PageInfo(list);
    }

    @Override
    public Syspush getContentByNoticeId(Long noticeId) {
        return syspushMapper.selectContetByNoticeId(noticeId);
    }

    @Override
    public int getSysnoticeByState(Long userId) {
        return sysnoticeMapper.selectSysnoticeByState(userId);
    }

    @Override
    public List<Sysnotice> getSysnotices(Long userId) {
        return sysnoticeMapper.selectSysnotices(userId);
    }

    @Override
    public void setSysnoticeState(int state, Long userId) {
        sysnoticeMapper.updataSysnoticeState(state,userId);
    }
}
